package edu.washington.ling.syntric.struct.rule;

import java.util.List;
import java.util.Iterator;

import edu.washington.ling.syntric.struct.tree.Node;


public class UniPSPattern implements Comparable<UniPSPattern> {
	private List<Node> psRootChain;
	public UniPSPattern(List<Node> rootChain) {
		psRootChain = rootChain;
	}
	public Node getRoot() {
		return psRootChain.get(0);
	}
	public Node getPreterminal() {
		return psRootChain.get(psRootChain.size() - 1);
	}
	public List<Node> getRootChain() {
		return psRootChain;
	}
	public boolean equals(Object other){
		if (this == other){
			return true;
		}
		if (!(other instanceof UniPSPattern)) {
			return false;
		}
		UniPSPattern otherPattern = (UniPSPattern) other;
		return (psRootChain.equals(otherPattern.psRootChain));
	}
	public int hashCode() {
		return psRootChain.hashCode();
	}
	@Override
	public int compareTo(UniPSPattern otherPatt) {
		Iterator<Node> psRootChainIter = psRootChain.iterator();
		Iterator<Node> otherPsRootChainIter = otherPatt.psRootChain.iterator();
		while (psRootChainIter.hasNext() && otherPsRootChainIter.hasNext()) {
			Node psNode = psRootChainIter.next();
			Node otherPSNode = otherPsRootChainIter.next();
			int psNodeCmp = psNode.compareTo(otherPSNode);
			if (psNodeCmp != 0) { return psNodeCmp; }
		}
		while (psRootChainIter.hasNext()) {
			Node psNode = psRootChainIter.next();
			Node otherPSNode = new Node();
			int psNodeCmp = psNode.compareTo(otherPSNode);
			if (psNodeCmp != 0) { return psNodeCmp; }
		}
		while (otherPsRootChainIter.hasNext()) {
			Node psNode = new Node();
			Node otherPSNode = otherPsRootChainIter.next();
			int psNodeCmp = psNode.compareTo(otherPSNode);
			if (psNodeCmp != 0) { return psNodeCmp; }
		}
		return 0;
	}
}
